package com.gene.service.impl;

import com.gene.mapper.TbItemCatMapper;
import com.gene.pojo.TbItemCat;
import com.gene.pojo.TbItemCatExample;
import com.gene.service.ITbitmeCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by lenovo on 2018/1/15.
 */
@Service
public class ITbitmeCarServiceImpl  implements ITbitmeCarService{

    @Autowired
    private TbItemCatMapper tbItemCatMapper;

    @Override
    public List<TbItemCat> query(Long parentId) {
        TbItemCatExample example=new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        criteria.andStatusEqualTo(1);
        List<TbItemCat> tbItemCats = tbItemCatMapper.selectByExample(example);
        return tbItemCats;
    }

    @Override
    public TbItemCat add(long parentId, String name) {
        TbItemCat cat=new TbItemCat();
        cat.setCreated(new Date());
        cat.setUpdated(new Date());
        cat.setIsParent(false);
        cat.setName(name);
        cat.setStatus(1);
        cat.setSortOrder(1);
        cat.setParentId(parentId);
        tbItemCatMapper.insert(cat);
        TbItemCat parentNode = tbItemCatMapper.selectByPrimaryKey(parentId);
        if(!parentNode.getIsParent()){
            parentNode.setIsParent(true);
            tbItemCatMapper.updateByPrimaryKey(parentNode);
        }
        return cat;
    }

    @Override
    public TbItemCat edit(TbItemCat cat) {
        cat.setUpdated(new Date());
        tbItemCatMapper.insert(cat);
        return cat;
    }

    @Override
    public void delete(Long id) {
        TbItemCatExample example=new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(id);
        TbItemCat cat=new TbItemCat();
        cat.setStatus(2);
        tbItemCatMapper.updateByExampleSelective(cat, example);
        TbItemCat cat2=new TbItemCat();
        cat2.setStatus(2);
        cat2.setId(id);
        tbItemCatMapper.updateByPrimaryKey(cat2);
    }



    /**
     * 查询商品二级目录  isParent=false   中的status=1的数据
     * 用于下来框渲染数据
     * @return
     */
    @Override
    public List<TbItemCat> twoQuery() {
        TbItemCatExample example=new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andIsParentEqualTo(false);
        criteria.andStatusEqualTo(1);
        example.setOrderByClause("created DESC");
        List<TbItemCat> list = tbItemCatMapper.selectByExample(example);
        return list;
    }



}
